<?php
// @author AliceXT
// 基于微信文章的采集
class Collect{
	public $countDB;
	public $sale;
	public $config = array();

	// 构造函数
	public function Collect(){
		
		$config['title_selector'] = "#activity-name";
		$config['content_selector'] = "#js_content";
		$config['img_selector'] = "img[data-src]";
		$this->config = $config;
        import('@.ORG.Collect.phpQuery');
	}
	
	private function setconfig($url){
		//腾讯新闻
		if(strpos($url, 'xw.qq.com') > 0){
			$config['title_selector'] = "h1.title:eq(0)";
		    $config['content_selector'] = ".content:eq(0)";
			$config['img_selector'] = "img[data-src]";
		}
		//微信文章 
		if(strpos($url, 'mp.weixin.qq.com') > 0){
			$config['title_selector'] = "#activity-name";
		    $config['content_selector'] = "#js_content";
			$config['img_selector'] = "img[data-src]";
		}
		
		//新浪
		if(strpos($url, 'sina.cn') > 0){
			$config['title_selector'] = "h1.art_title_h1:eq(0)";
		    $config['content_selector'] = ".art_main_card.j_article_main > *:gt(0)";
			$config['img_selector'] = "img[data-src]";
		}
		
		
		
		//快报 
		if(strpos($url, 'view.inews.qq.com') > 0){
			$config['title_selector'] = ".main#content > .title:eq(0)";
		    $config['content_selector'] = ".main#content > *:gt(1)";
			$config['img_selector'] = "img[data-src]";
		}
		
		
		$this->config = $config;
	}
	
	
	
	
	
	/*
	通过微信文章地址获得一个数组，数组包括文章标题和文章内容

	@param string url 微信文章地址
	@return array 数组包括状态、文章标题和文章内容
	*/

	public function collectUrl($url = ""){
		$this -> setconfig($url);
		$config = $this->config;

        $html = preg_replace("/<!--[\w\W\r\n]*?-->/",'',file_get_contents($url));
		$find     = array("~>\s+<~","~>(\s+\n|\r)~");
		$replace  = array("><",">");
		$html = preg_replace($find, $replace, $html);
		
		
		
		
		if(strpos($url, 'mp.weixin.qq.com') > 0){
			//找到页面描述
			$pageinfo = array();
			preg_match_all('/msg_desc \= ".+/', $html, $pageinfo);
			$ret['page_desc'] = $pageinfo[0][0];
			$ret['page_desc'] = str_replace('";','',$ret['page_desc']);
			$ret['page_desc'] = trim(str_replace('msg_desc = "','',$ret['page_desc']));
			$pageinfo = array();
			preg_match_all('/msg_title \= ".+/', $html, $pageinfo);
			$ret['page_title'] = $pageinfo[0][0];
			$ret['page_title'] = str_replace('";','',$ret['page_title']);
			$ret['page_title'] = str_replace('msg_title = "','',$ret['page_title']);
			$pageinfo = array();
			preg_match_all('/msg_cdn_url \= ".+/', $html, $pageinfo);
			$ret['img_url'] = str_replace('";','',$pageinfo[0][0]);
			$ret['img_url'] = str_replace('msg_cdn_url = "','',$ret['img_url']);
			$ret['img_url'] = $this->getImage(trimall($ret['img_url']), "./Public/Uploads/".__SITEID__."/Collect".__SITEID__."-".date("Ymd")."/".time().'.jpg');
			$ret['img_url'] = substr($ret['img_url'],8);
		}
		
		//微信文章 
		if(strpos($url, 'xw.qq.com') > 0){
			//找到页面描述
			$pageinfo = array();
			preg_match_all('/desc\: ".+/', $html, $pageinfo);
			$ret['page_desc'] = $pageinfo[0][0];
			$ret['page_desc'] = str_replace('",','',$ret['page_desc']);
			$ret['page_desc'] = trim(str_replace('desc: "','',$ret['page_desc']));
			$pageinfo = array();
			preg_match_all('/title\: ".+/', $html, $pageinfo);
			$ret['page_title'] = $pageinfo[0][0];
			$ret['page_title'] = str_replace('",','',$ret['page_title']);
			$ret['page_title'] = str_replace('title: "','',$ret['page_title']);
			$pageinfo = array();
			preg_match_all('/img\: ".+/', $html, $pageinfo);
			$ret['img_url'] = str_replace('",','',$pageinfo[0][0]);
			$ret['img_url'] = str_replace('img: "','',$ret['img_url']);
			$ret['img_url'] = $this->getImage(trimall($ret['img_url']), "./Public/Uploads/".__SITEID__."/Collect".__SITEID__."-".date("Ymd")."/".time().'.jpg');
			$ret['img_url'] = substr($ret['img_url'],8);
		}
		
		//腾讯新闻
		if(strpos($url, 'view.inews.qq.com') > 0){
			//找到页面描述
			$pageinfo = array();
			preg_match_all('/"desc"\: \'.+/', $html, $pageinfo);
			$ret['page_desc'] = $pageinfo[0][0];
			$ret['page_desc'] = str_replace('\',','',$ret['page_desc']);
			$ret['page_desc'] = trim(str_replace('"desc": \'','',$ret['page_desc']));
			$pageinfo = array();
			preg_match_all('/"title"\: \'.+/', $html, $pageinfo);
			$ret['page_title'] = $pageinfo[0][0];
			$ret['page_title'] = str_replace('\',','',$ret['page_title']);
			$ret['page_title'] = str_replace('"title": \'','',$ret['page_title']);
			$pageinfo = array();
			preg_match_all('/"img_url"\: ".+/', $html, $pageinfo);
			$ret['img_url'] = str_replace('",','',$pageinfo[0][0]);
			$ret['img_url'] = str_replace('"img_url": "','',$ret['img_url']);
			$ret['img_url'] = $this->getImage(trimall($ret['img_url']), "./Public/Uploads/".__SITEID__."/Collect".__SITEID__."-".date("Ymd")."/".time().'.jpg');
			$ret['img_url'] = substr($ret['img_url'],8);
		}
		//新浪
		if(strpos($url, 'sina.cn') > 0){
			//找到页面描述
			$pageinfo = array();
			preg_match_all('/__content:\'.+/', $html, $pageinfo);
			$ret['page_desc'] = $pageinfo[0][0];
			$ret['page_desc'] = str_replace('\',','',$ret['page_desc']);
			$ret['page_desc'] = trim(str_replace('__content:\'','',$ret['page_desc']));
			$pageinfo = array();
			preg_match_all('/__title:\'.+/', $html, $pageinfo);
			$ret['page_title'] = $pageinfo[0][0];
			$ret['page_title'] = str_replace('\',','',$ret['page_title']);
			$ret['page_title'] = str_replace('__title:\'','',$ret['page_title']);
			$pageinfo = array();
			preg_match_all('/__mainPic:\'.+,/', $html, $pageinfo);
			$ret['img_url'] = str_replace('\',','',$pageinfo[0][0]);
			$ret['img_url'] = str_replace('__mainPic:\'','',$ret['img_url']);
			$ret['img_url'] = $this->getImage(trimall($ret['img_url']), "./Public/Uploads/".__SITEID__."/Collect".__SITEID__."-".date("Ymd")."/".time().'.jpg');
			$ret['img_url'] = substr($ret['img_url'],8);
		}
		
		
		//dump($ret);exit;
		//$html='';
        if(!empty($html)){
			// 导入phpQuery
        	$html = phpQuery::newDocumentHTML($html);
        	// 获得标题
        	$ret['title'] = trim(pq($config['title_selector'])->html());
        	// 获得内容
        	$content = '';
			$cdo = pq($config['content_selector']);
			//dump($cdo);exit;
			//dump($cdo);exit;
			//if(is_array($cdo)){
			if(is_array($config['content_selector'])){
				foreach($config['content_selector'] as $val){
					foreach(pq($val) as $v){
						$html = pq($v) -> html();
						$content .= '<p>'.$html.'</p>';
					}
				}
				
			}else{
				$cdo = pq($config['content_selector']);
				$i = count($cdo);
				foreach($cdo as $k => $v){
					//新浪排除最后两个元素
					$i --;
					if(strpos($url, 'sina.cn') > 0 && $i <= 5){
						continue;
					}
					$html = pq($v) -> html();
					$content .= '<p>'.$html.'</p>';
				}
			}
			
			
        	// echo $content;
        	$ret['content'] = $this->handleImg($content);
        	// 保存状态
        	$ret['status'] = $ret['title'] && $ret['content'] ? true : false;
        }else{
        	$ret['status'] = false;
        }
        return $ret;
	}

	/*
	将内容里的图片保存在服务器本地，并且替换$content里的img标签

	@param string content 微信文章正文内容
	@return string img标签图片处理的内容
	*/
	public function handleImg($content){
		$config = $this->config;
		$handle = $content;
		// 形成保存路径
		$savePath = "./Public/Uploads/".__SITEID__."/Collect".__SITEID__."-".date("Ymd")."/";
		// 添加保存目录
		mk_dir($savePath);
		// 查找img标签
		$imgs = pq($config['img_selector']);
		foreach($imgs as $k=>$img){
			// 获取图片路径
			$data_src = pq($img)->attr("data-src");

			// 保存图片到本地
			// 获取后缀
			$ext=substr(strrchr($data_src,'='),1);
			// 获得文件名
			$filename = date("YmdHis") . '_' . rand(10000, 99999) . ".".$ext;//按照系统统一的格式去创建新的文件
			// 获得新文件路径
			$src = $this->getImage($data_src,$savePath.$filename);
			// 新旧图片src的替换
			$old_html = 'data-src="'.$data_src.'"';
			$new_html = 'src="'.substr($src,1).'"';
			$handle = str_replace($old_html,$new_html,$handle);
		}
		
		
		$ret = array();
        preg_match_all("/url\(http:\/\/[a-zA-Z0-9\?\=\._\/)]+/", $handle, $ret);
		foreach($ret[0] as $k => $v){
			$url = $v;
			$url = str_replace('url(','',$url);
			$url = str_replace(')','',$url);
			$filename = date("YmdHis") . '_' . rand(10000, 99999) . ".jpg";
			$src = 'url('.substr($this->getImage($url,$savePath.$filename),1).')';
			$handle = str_replace($v,$src,$handle);
		}
		
		return $handle;
	}

	// 上传图片到$filename
	private function getImage($url,$filename='',$type=0){
	    if($url==''){return false;}
	    if($filename==''){
	        $ext=substr(strrchr($url,'='),1);
	        // if($ext!='.gif' && $ext!='.jpg' && $ext != '.jpeg' && $ext){return false;}
	        $filename=time().'.'.$ext;
	    }
	    //文件保存路径 
	    if($type){
	  $ch=curl_init();
	  $timeout=5;
	  curl_setopt($ch,CURLOPT_URL,$url);
	  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	  $img=curl_exec($ch);
	  curl_close($ch);
	    }else{
	     ob_start(); 
	     readfile($url);
	     $img=ob_get_contents(); 
	     ob_end_clean(); 
	    }
	    $size=strlen($img);
	    //文件大小 
	    $fp2=@fopen($filename,'a');
	    fwrite($fp2,$img);
	    fclose($fp2);
	    return $filename;
	}
}